package com.yangxk.kadmin.common.util;

import com.yangxk.kadmin.common.exception.ParamException;

import java.util.Collection;

/**
 * 校验类
 *
 * @author yangxk
 * @date 2018/9/12 15:14
 */
public class ValidUtil {

    private ValidUtil(){}

    /**
     * 验证字符串是否为空,
     * create by yangxk on 2018/9/12 15:15
     */
    public static boolean isEmpty(String str) {
        if (str == null || "".equals(str)) {
            return true;
        }

        return false;
    }

    /**
     * 验证集合是否为空
     * create by yangxk on 2018/9/12 15:17
     */
    public static boolean isEmpty(Collection<?> collection) {
        if (collection == null || collection.isEmpty())
            return true;
        return false;
    }

    /**
     * 验证数组是否为空
     * create by yangxk on 2018/9/12 15:21
     */
    public static boolean isEmpty(Object[] arr) {
        if (arr == null || arr.length == 0)
            return true;
        return false;
    }

    /**
     * 检查指定对象是否为空
     * 如果为null, 则直接抛出异常
     * create by yangxk on 2018/9/19 16:17
     */
    public static void checkNotNull(Object object, String message) {
        if (object instanceof String) {
            String str = (String) object;
            if (ValidUtil.isEmpty(str))
                throw new ParamException(message);
        }

        if (object == null)
            throw new ParamException(message);
    }
}
